﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XiaoMan.Model.Models;
using System.Data.Entity;

namespace XiaoMan.DAL
{
    public class BaseDAO<T> where T : class
    {
        public XiaoManS10Context context = DbContextFactory.GetDbContext();
        //通用的添加的方法
        public void Add(T entity)
        {
            context.Set<T>().Add(entity);
        }
        public void Delete(T entity)
        {
            context.Set<T>().Remove(entity);
        }
        public void Updata(T entity, T newStudent)
        {
            context.Entry(entity).State = EntityState.Detached;
            context.Entry(newStudent).State = EntityState.Modified;
        }
        public List<T> GetList(Func<T, bool> LambadaWhere)
        {
            return context.Set<T>().Where(LambadaWhere).ToList();
        }
        public List<T> GetListByPage<S>(Func<T, bool> lambadaWhere, Func<T, S> lambadaOrder, bool isAsc, int pageIndex, int pageSize, ref int pageCount)
        {
            List<T> temp = context.Set<T>().Where(lambadaWhere).ToList();
            pageCount = temp.Count;
            if (isAsc)
            {
                return temp.OrderBy(lambadaOrder)
                            .Skip((pageIndex - 1) * pageSize)
                            .Take(pageSize).ToList();
            }
            else
            {
                return temp.OrderByDescending(lambadaOrder)
                            .Skip((pageIndex - 1) * pageSize)
                            .Take(pageSize).ToList();
            }
        }
    }
}
